package hu.bme.gabor.onlab;


import android.app.TabActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.ToggleButton;

public class Light extends TabActivity implements TabHost.TabContentFactory {
	
	private Communication comm;
	TabHost tabHost;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_light_scroll);
        
        comm = new Communication(); 
        
        tabHost = getTabHost();

        for (int i=1; i < 9; i++) {
            String name = "Lamp " + i;
            tabHost.addTab(tabHost.newTabSpec(name).setIndicator(name).setContent(this));
            tabHost.getTabWidget().getChildAt(i-1).setLayoutParams(new LinearLayout.LayoutParams(160, 100));
        }
        
    }

    /** {@inheritDoc} */
    public View createTabContent(final String tag) {

        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = vi.inflate(R.layout.layout_light, null);
		
		final ToggleButton btn = (ToggleButton) view.findViewById(R.id.toggleButton1);
		final ImageView img = (ImageView) view.findViewById(R.id.lamp);
		
		if(btn.isChecked()){
			img.setImageResource(R.drawable.lamp_on);
		}else{
			img.setImageResource(R.drawable.lamp_off);
		}
		
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(btn.isChecked()){
					img.setImageResource(R.drawable.lamp_on);
					comm.sendDataToAvr(btn.isChecked(), tabHost.getCurrentTab());
				}else{
					img.setImageResource(R.drawable.lamp_off);
					comm.sendDataToAvr(btn.isChecked(), tabHost.getCurrentTab());
				}
			}
		});
    	
        return view;
    }
}